﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SumiaAccountBook.SubControls
{
    /// <summary>
    /// UCSearch_ByStaff.xaml 的交互逻辑
    /// </summary>
    public partial class UCSearch_ByStaff : UserControl
    {
        const int PageSize = 20;
        private int page_count = 1;

        Biz.KVAccountBook kvAccount = new Biz.KVAccountBook();
        Biz.KVStaff kvStaff = new Biz.KVStaff();

        List<Model.StafftSum> SumList = new List<Model.StafftSum>();
        List<Model.IExporSource> ExportList = new List<Model.IExporSource>();

        public UCSearch_ByStaff()
        {
            InitializeComponent();

            var books = this.kvAccount.GetAllValue();
            this.cb_accountbook.ItemsSource = books;
            if (books != null && books.Count > 0)
            {
                this.cb_accountbook.SelectedIndex = 0;
            }

            bindData();
            this.DataContext = this;
        }

        private void bindData()
        {
            DateTime sd = DateTime.MinValue;
            if (!string.IsNullOrEmpty(this.dp_date_start.Text.Trim()))
            {
                sd = Convert.ToDateTime(this.dp_date_start.Text.Trim());
            }
            DateTime ed = DateTime.MinValue;
            if (!string.IsNullOrEmpty(this.dp_date_end.Text.Trim()))
            {
                ed = Convert.ToDateTime(this.dp_date_end.Text.Trim());
            }
            int AccountBookId = -1;
            if (!string.IsNullOrEmpty(this.cb_accountbook.SelectedValue as string))
            {
                AccountBookId = this.kvAccount.GetKey(this.cb_accountbook.SelectedValue as string);
            }

            var listCashFlow = DataAccess.DaCashFlow.GetStaffList(AccountBookId, sd, ed);
            Dictionary<int, Model.StafftSum> dict = new Dictionary<int, Model.StafftSum>();                    
            if (null != listCashFlow && listCashFlow.Count > 0)
            {
                foreach (var data in listCashFlow)
                {
                    if (dict.ContainsKey(data.StaffId))
                    {
                        Model.StafftSum sum = dict[data.StaffId];
                        if(data.Type==(int)Biz.KVCashFlowType.emCashFlowType.In)
                        {
                            sum.Income += data.Amount;
                        }
                        else
                        {
                            sum.Outcome += data.Amount;
                        }
                    }
                    else
                    {
                        if (data.Type == (int)Biz.KVCashFlowType.emCashFlowType.In)
                        {
                            dict.Add(data.StaffId,
                                new Model.StafftSum
                                {
                                    StaffId = data.StaffId,
                                    Staff = this.kvStaff.GetValue(data.StaffId),                                    
                                    Income = data.Amount
                                });
                            
                        }
                        else
                        {
                            dict.Add(data.StaffId,
                                new Model.StafftSum
                                {
                                    StaffId = data.StaffId,
                                    Staff = this.kvStaff.GetValue(data.StaffId),                                    
                                    Outcome = data.Amount
                                });
                        }
                    }
                }
            }

            this.SumList.Clear();
            this.SumList = dict.Values.ToList();
            this.SumList = this.SumList.OrderBy(r => r.StaffId).ToList();                        
            this.dp.TotalCount = this.SumList.Count();
            this.ExportList.Clear();
            foreach (var v in this.SumList)
            {
                this.ExportList.Add(v);
            }
            var totalIncome = this.SumList.Sum(d => d.Income);
            var totalOutcome = this.SumList.Sum(d => d.Outcome);
            this.lb_sm.Text = "收入:" + totalIncome + ",支出:" + totalOutcome;

            showPage();

        }

        private void btn_search_Click(object sender, RoutedEventArgs e)
        {
            bindData();
        }

        private void btn_export_Click(object sender, RoutedEventArgs e)
        {
            if (null == ExportList || ExportList.Count <= 0)
            {
                MessageBox.Show("没有数据可导出。");
                return;
            }
            var dialog = new SaveFileDialog();

            dialog.Title = "请选择文件位置";
            dialog.Filter = "CSV(*.CSV)|*.CSV";
            dialog.InitialDirectory = "d:\\";
            dialog.FileName = string.Format("按人员统计{0}.csv", DateTime.Now.ToString("yyyyMMdd"));
            //获得另存为的文件名称
            if (dialog.ShowDialog() == true)
            {
                ExportToFilecs.Export(dialog.FileName, this.ExportList);
            }
        }

        private void dp_PageChanged(object sender, CubicFamily.StockTool.ComonControl.PageChangedEventArgs e)
        {
            this.page_count = e.CurrentPageIndex;
            showPage();
        }

        private void showPage()
        {
            if (this.SumList.Count > 0)
            {
                this.dg.ItemsSource = this.SumList.Skip(PageSize * (this.page_count - 1)).Take(PageSize);
            }
            else
            {
                this.dg.ItemsSource = null;
            }
        }

    }
}
